package com.shadt.cloud.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.shadt.cloud.service.BranchMService;
import com.shadt.cloud.vo.BranchVo;
import com.shadt.core.controller.BaseController;
import com.shadt.core.model.Json;

/**
 * 分平台控制器
 * 
 * @author Maogc
 *
 */
@Controller
@RequestMapping(value = "/mexamine")
public class BranchMController extends BaseController {
	@Autowired
	private BranchMService BMservice;
	/**
	 * 跳转到登录页面
	 * @return
	 */
	@RequestMapping("login")
	public String login(){
		return "qczlm/jsp/login";
	}
	/**
	 * 主页面
	 * @return
	 */
	@RequestMapping("m_index")
	public String mainIndex(){
		return "qczlm/jsp/main_index";
	}
	/**
	 * 状态修改为0  
	 * @param id 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/update",method={RequestMethod.GET,RequestMethod.POST})
	public boolean update(int branch_id,int id,int status){
		boolean flag = BMservice.update(branch_id,id,status);
		return flag;
	}
	
	/**
	 * 测试    输入手机号码获取验证码
	 * @param phone 电话号码
	 * @return
	 */
	@ResponseBody	
	@RequestMapping(value="/send",method={RequestMethod.GET})
	public String sendMsg(String phone){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("msg", "ok");
		map.put("obj", "7043");
		map.put("success", true);
		Object json = JSON.toJSON(map);
		return json.toString();
	}
	/**
	 * 测试		验证码是否正确
	 * @param phone
	 * @param code
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/checkcode",method={RequestMethod.GET,RequestMethod.POST})
	public Json checkCode(String phone,String code){
		Json j = new Json();
		if(code.equals("1")){
			j.setSuccess(true);
		}else{
			j.setSuccess(false);
		}
		return j;
	}
	/**
	 * 跳转页面专用方法---------------------------------
	 */
	//权限申请列表页面
	@RequestMapping(value="/locaflm")
	public String aSLocationflm(){
		return "qczlm/easyui/main_flm";
	}
	//权限审核页面
	@RequestMapping(value="/chlist_jsp")
	public String channe_jsp(){
		return "qczlm/jsp/main_qx";
	}
	//查询云平台数据
	@RequestMapping(value="/qyun")
	public String channe_yun(){
		return "qczlm/jsp/main_yunQ";
	}
	/**--------------------------------------------------*/
	/**
	 * 退出登录
	 */
	@RequestMapping(value="/logout")
	public String logout(HttpSession session){
		session.invalidate();
		return "redirect:login";
	}
	/**
	 * 权限申请列表
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/chlist")
	public String channelList(){
		String page = getRequest().getParameter("page");
		String pnum = getRequest().getParameter("pnum");
		String type = getRequest().getParameter("type"); //搜索类型
		String search = getRequest().getParameter("search"); //搜索内容
		if(search!=null){
			search=search.replaceAll("\\s", "");
		}
		String list=BMservice.channelList(Integer.valueOf(page),Integer.valueOf(pnum),type,search);
		String newList=null;
		List<String> listArr = null;
		if(list!=null){
			String arr=list.substring(0,list.lastIndexOf(","));
			String count=list.substring(list.lastIndexOf(",")+1,list.length());
			listArr = new ArrayList<String>();
			String a = String.valueOf(arr.charAt(0));
			String b = String.valueOf(arr.charAt(1));
			if(a.equals("[") && b.equals("[")){
				arr = arr.substring(1,arr.length()-1);
			}
			listArr.add(arr);
			listArr.add(count);
			newList=listArr.toString();
		}
		return newList;
	}
	/**
	 * 时间转换成时间戳
	 * @param time
	 * @return
	 */
	public static String dateTime(String time){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String res=null;
        Date date=null;
		try {  
			date = sdf.parse(time);
			long ts = date.getTime()/1000;
	        res = String.valueOf(ts);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return res;
	}
	
	
	
	/**
	 * 登录方法
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(value="/login_user")
	public boolean loginuser(HttpSession session){
		String phone = getRequest().getParameter("phone");//用户
		String password=getRequest().getParameter("password");//密码
		String utype=getRequest().getParameter("utype");//登录身份
		BranchVo vo=null;
		String adminjson=null;
		//返回查询记录
		if(utype!=null && utype.equals("0")){
			//管理员
			adminjson = BMservice.adminLogin(phone, password);
		}else{
			//普通用户
			vo=BMservice.login(phone, password);
		}
		Map<String, Object> map = null;
		if(vo!=null){
			map=(Map<String, Object>) JSON.toJSON(vo);
			session.setAttribute("list", map);
			session.setAttribute("username", map.get("username"));//用户名
			session.setAttribute("secret", map.get("secret"));
			session.setAttribute("name", map.get("name"));//分平台名称
			session.removeAttribute("type");
		}else if(adminjson!=null){
			map = JSONObject.parseObject(adminjson);
			session.setAttribute("username", map.get("name"));//管理员
			session.setAttribute("type",map.get("type"));//账号类型
			session.removeAttribute("list");
		}else{
			return false; 
		}
		return true;
	}
}
